<template xmlns="">
  <div>
  <el-container class="layout-container-demo" style="height: 100vh">

    <el-container>
      <el-header style="text-align: right; font-size: 12px">
        <div class="toolbar">
          <el-dropdown>
            <el-icon style="margin-right: 8px; margin-top: 1px"
            ><setting
            /></el-icon>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="$refs.AdministratorEdit.administratorEditVisible=true">个人信息
                </el-dropdown-item>
                <el-dropdown-item @click="$router.push('/')">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          <span>Tom</span>
        </div>
      </el-header>
<el-container>
  <el-aside width="200px">
    <el-scrollbar>
      <el-menu :default-openeds="['1', '2']">
        <el-sub-menu index="1">
          <template #title>
            <el-icon>
              <message/>
            </el-icon>
            订单
          </template>
          <el-menu-item index="1-1" @click="controllerMain('order')">管理订单</el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="2">
          <template #title>
            <el-icon>
              <message/>
            </el-icon>
            客户
          </template>
          <el-menu-item index="1-1" @click="controllerMain('customer');queryAllCustomer()">客户信息</el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-scrollbar>
  </el-aside>
      <el-main>
        <div style="height: 60vh;margin: 50px;" v-if="mainContent.order">
          <el-table :data="orderList">
            <el-table-column prop="id" label="订单编号" width="100" />
            <el-table-column prop="customerName" label="用户名" width="120" />
            <el-table-column prop="time" label="下单时间" />
            <el-table-column prop="totalPrice" label="总价格" width="140" />
            <el-table-column prop="carImage" label="汽车实物图" width="200">
              <template #default="scope">
                <el-image style="width: 100px; height: 100px" :src="scope.row.carImage" :fit="fit"/>
              </template>
            </el-table-column>
            <el-table-column prop="carName" label="汽车名字" />
            <el-table-column
                prop="tag"
                label="标签"
                width="100"
                :filters="filters"
                :filter-method="filterTag"
                filter-placement="bottom-end">
              <template #default="scope">
                <el-tag
                    :type="scope.row.status === '已支付' ? 'primary' : 'success'"
                    disable-transitions>{{scope.row.status}}</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" style="align-content: center">
              <template #default="scope">
                <el-button size="small"
                           @click="$refs.OrderEdit.orderEditVisible=true;$refs.OrderEdit.updateOrder(scope.row)">
                  Edit
                </el-button>
                <el-popconfirm title="Are you sure to delete this?"
                               @confirm="deleteOrder(scope.row)">
                  <template #reference>
                    <el-button size="small" type="danger">Delete</el-button>
                  </template>
                </el-popconfirm>
                <el-button size="small"
                           @click="$refs.OrderEdit.orderEditVisible=true;$refs.OrderEdit.updateOrder(form)">添加
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div style="height: 60vh;margin: 50px;" v-if="mainContent.customer">
          <el-table :data="customerList">
            <el-table-column prop="id" label="客户编号" width="100"/>
            <el-table-column prop="name" label="客户姓名" width="120"/>
            <el-table-column prop="phone" label="联系方式"/>
            <el-table-column prop="address" label="住址" width="140"/>
            <el-table-column prop="sex" label="性别" width="200"/>
            <el-table-column label="操作" style="align-content: center">
              <template #default="scope">
                <el-button size="small"
                           @click="$refs.CustomerEdit.customerEditVisible=true;$refs.CustomerEdit.updateCustomer(scope.row)">
                  Edit
                </el-button>
                <el-popconfirm title="Are you sure to delete this?"
                               @confirm="deleteCustomer(scope.row)">
                  <template #reference>
                    <el-button size="small" type="danger">Delete</el-button>
                  </template>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-main>
</el-container>
    </el-container>
  </el-container>
    <OrderEdit ref='OrderEdit'/>
    <CustomerEdit ref='CustomerEdit'/>
    <AdministratorEdit ref="AdministratorEdit"/>
  </div>
</template>
<script  setup>
import {ref} from 'vue'

import OrderEdit from "@/components/OrderEdit.vue";
import CustomerEdit from "@/components/CustomerEdit.vue"
import AdministratorEdit from "@/components/AdministratorEdit.vue"
import {deleteCustomerByIdApi, queryAllCustomerApi} from "@/axios/api/Customer";
import {deleteOrderByIdApi, queryAllOrderApi} from "@/axios/api/Order";
import {Message, Setting} from "@element-plus/icons-vue";

const fit = "contain"
const mainContent = ref({
  order: true,
  customer: false,
})
const form = {
  id: '',
  customerName: '',
  customerPhone: '',
  time: '',
  totalPrice: '',
  status: '',
  carName: '',
  carImage: '',
}

const item =
  {
    "id": 26,
    "customerName": "江直着成二际",
    "customerPhone": "18678424225",
    "time": "2018-01-11 22:35:29",
    "status": "已支付",
    "totalPrice": 21.0,
    "carImage": 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
    "carName": null
  }
const customerList = ref([{
  "id": 1,
  'name': "张三",
  "phone": "1561000",
  "address": "上海",
  "sex": "男"
},
  {
    "id": 1,
    'name': "张三",
    "phone": "1561000",
    "address": "上海",
    "sex": "男"
  }])
const orderList = ref(Array.from({length: 20}).fill(item))
const filters=ref([
  { text: '已支付', value: '已支付' }, { text: '未支付', value: '未支付' }
])
const controllerMain = (target) => {
  // 遍历对象的所有属性
  for (let key in mainContent.value) {
    // 判断属性是否为 'name'
    mainContent.value[key] = key === target;
  }
  // console.log(mainContent.value)
}
const filterTag=(value, row)=> {
  return row.status === value;
}
const deleteOrder = (row) => {
  deleteOrderByIdApi({id: row.id}).then(response => {
    queryAllOrder()
  })
}
const deleteCustomer = (row) => {
  deleteCustomerByIdApi({id: row.id}).then(response => {
    queryAllOrder()
  })
}
const queryAllCustomer = () => {
  queryAllCustomerApi().then(response => {
    customerList.value = response
    console.log(customerList.value)
  })
}
queryAllCustomer()
const queryAllOrder = () => {
  queryAllOrderApi().then(response => {
    orderList.value = response
    console.log(response)
  })
}
queryAllOrder()

</script>



<style scoped>
.layout-container-demo .el-header {
  position: relative;
  background-color: var(--el-color-primary-light-7);
  color: var(--el-text-color-primary);
}
.layout-container-demo .el-aside {
  color: var(--el-text-color-primary);
  background: var(--el-color-primary-light-8);
}
.layout-container-demo .el-menu {
  border-right: none;
}
.layout-container-demo .el-main {
  margin: 50px;
  border: 1px solid #f6c974; /* Border color and thickness */
  border-radius: 10px;
  padding: 0;
}
.layout-container-demo .el-table {
  height: 50vh; /*设置表格高度 */
}
.layout-container-demo .toolbar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  right: 20px;
}
</style>